package com.gaofei.controller;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gaofei.domain.Spec;
import com.gaofei.domain.SpecOption;
import com.gaofei.service.SpecService;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("spec")
public class SpecController {

	@Reference
	SpecService specService;
	
	
	@RequestMapping("findSpecOptionById")
	@ResponseBody
	public Object findSpecOptionById(Integer specId) {
			List<SpecOption> optionList = specService.findSpecOptionBySpecId(specId);
			return optionList;
	}
	
	
	@RequestMapping("update")
	@ResponseBody
	public boolean update(Spec spec ,Model m) {
		try {
			specService.update(spec);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	// 跳转到添加页面
	@RequestMapping("toUpdate")
	public String toUpdate(Integer id ,Model m) {
		Spec spec = specService.findSpecById(id);
		m.addAttribute("spec", spec);
		return "spec/update";
	}

	// 跳转到添加页面
	@RequestMapping("add")
	@ResponseBody
	public boolean add(Spec spec) {
		try {

			specService.add(spec);
			return true;
		} catch (Exception e) {
			return false;
		}

	}

	// 跳转到添加页面
	@RequestMapping("toAdd")
	public String toAdd() {
		return "spec/add";
	}

	// 批量删除
	@RequestMapping("delBach")
	@ResponseBody
	public boolean del(String ids) {

		try {
			specService.del(ids);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@RequestMapping("list")
	public String list(Model m, @RequestParam(defaultValue = "1") int pageNum,
			@RequestParam(defaultValue = "5") int pageSize) {

		PageInfo<Spec> info = specService.list(pageNum, pageSize);
		System.out.println(info.getList());
		m.addAttribute("info", info);
		return "spec/list";
	}
}
